//
//  account.c
//  c-game
//
//  Created by Gao YuShan on 2022/10/29.
//



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "game.h"
#include "account.h"

const int TRUE = 1;
const int FALSE = 0;

void readString(char *result, char *message)
{
    printf("%s.\n", message);
    scanf("%s", result);
}

AccountNode *createAccountNode(void)
{
    AccountNode *h = (AccountNode *)malloc(sizeof(AccountNode));
    h->next = NULL;
    return h;
}

AccountNode *readAccount(FILE *fp)
{
    AccountNode *a = createAccountNode();
    if(access("accountRegister.txt",F_OK)==-1)
    {
        return NULL;
    }
    int i = fscanf(fp, "%s", a->data.id);
    fscanf(fp, "%s", a->data.name);
    fscanf(fp, "%s", a->data.tel);
    fscanf(fp, "%s", a->data.password);
    fscanf(fp, "%d", &a->data.winsNumber);
    if (i != 1)
    {
        free(a);
        return NULL;
    }
    else
    {
        return a;
    }
}

AccountNode *loadAccount(void)
{
    AccountNode *accounts = createAccountNode();
    system("pwd");
    FILE *fp = fopen("./accountRegister.txt", "r");

    AccountNode *lastAccount = accounts;
    while (TRUE)
    {
        AccountNode *newAccount = readAccount(fp);
        if (newAccount == NULL)
        {
            break;
        }

        lastAccount->next = newAccount;
        lastAccount = newAccount;
    }

    fclose(fp);
    return accounts;
}

AccountNode *findPreaccount(char *name, AccountNode *h)
{
    AccountNode *p = h;
    while (p->next != NULL)
    {
        if (strcmp(name, p->next->data.name) == 0)
        {
            return p;
        }
        p = p->next;
    }
    return NULL;
}

int deleteAccount(char *name, AccountNode *h)
{
    AccountNode *p = findPreaccount(name, h);
    if (p == NULL)
    {
        return FALSE;
    }

    AccountNode *next = p->next->next;
    free(p->next);
    p->next = next;
    return TRUE;
}

void saveAccount(AccountNode *h)
{
    FILE *fp = fopen("./accountRegister.txt", "w");

    AccountNode *p = h->next;
    while (p != NULL)
    {

        fprintf(fp, "%s ", p->data.id);
        fprintf(fp, "%s ", p->data.name);
        fprintf(fp, "%s ", p->data.tel);
        fprintf(fp, "%s\n", p->data.password);
        fprintf(fp, "%d\n", p->data.winsNumber);
        p = p->next;
    }
    fclose(fp);
    return;
}

AccountNode *getNewAccount(void)
{
    AccountNode *a = createAccountNode();
    printf("请输入新的身份证号,姓名(不超过20个字符),手机号和登录密码(不超过10个字符).\n");
    scanf("%s %s %s %s", a->data.id, a->data.name, a->data.tel, a->data.password);
    a->data.winsNumber=0;
    return a;
}

void modifyAccount(char *orgName, AccountNode *newAccount, AccountNode *accounts)
{
    AccountNode *p = findPreaccount(orgName, accounts);
    if (p == NULL)
    {
        return;
    }
    AccountNode *tmp = p->next;
    newAccount->next = tmp->next;
    p->next = newAccount;
    free(tmp);
}

void modifySomeAccount(void)
{
    AccountNode *accounts = loadAccount();

    char name[21];
    readString(name, "请输入要修改账户的名字");
    AccountNode *newAccount = getNewAccount();

    modifyAccount(name, newAccount, accounts);

    saveAccount(accounts);
}

void deleteSomeAccount(void)
{
    AccountNode *h = loadAccount();

    char name[21];
    readString(name, "请输入要删除账户的名字");
    deleteAccount(name, h);
    saveAccount(h);
}

void zhuce(void)
{
    // 录入用户信息
    // 加载用户列表
    // 在用户列表中查找录入的用户
    // 如果找到了，则提示用户名称已存在，注册失败；
    // 否则将用户添加到用户列表，并保存
    char a = 'a';
    while (a != 'n')
    {
        AccountNode *account = getNewAccount();

        AccountNode *accounts = loadAccount();

        if (findPreaccount(account->data.name, accounts) == NULL)
        {
            account->next = accounts->next;
            accounts->next = account;
            saveAccount(accounts);
        }
        else
        {
            printf("名称已存在.\n");
        }

        readString(&a, "是否具续登录.是请回答y,否请回答n");
    }
}

AccountNode *denglu(void)
{
    char name[21], password[11];
    readString(name, "请输入姓名");
    getchar();
    readString(password, "请输入密码");

    AccountNode *accounts = loadAccount();
    AccountNode *matched = findPreaccount(name, accounts);

    if (matched != NULL && strcmp(password, matched->next->data.password) == 0)
    {
        printf("登录成功\n");
        return matched->next;
    }
    else
    {
        printf("账号或密码错误,登录失败,请重新登录\n");
        return NULL;
    }
}
